package br.cin.stadium.middleware.media;

import br.cin.stadium.middleware.FormatException;
import br.cin.stadium.middleware.marshall.MarshallInputStream;
import br.cin.stadium.middleware.marshall.MarshallOutputStream;

/**
 * This class represents a SPACE ENTRY for Stadium Middleware.
 * Only this class can be written.
 * 
 * @author Fabiano, Jorge, Rafael, Vando
 * 
 */
public class Media extends ObjectSerializable
{

    private String name;
    private String type;
    private int    stadiumSector;
    private byte[] content;

    public Media(String name, String type, int stadiumSector, byte[] content)
    {
        super();
        this.name = name;
        this.type = type;
        this.content = content;
        this.stadiumSector = stadiumSector;
    }

    public Media()
    {
        // empty
    }

    public String getName()
    {
        return name;
    }

    public int getStadiumSector()
    {
        return stadiumSector;
    }

    public String getType()
    {
        return type;
    }

    public byte[] getContent()
    {
        return content;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public void setType(String type)
    {
        this.type = type;
    }

    public void setStadiumSector(int stadiumSector)
    {
        this.stadiumSector = stadiumSector;
    }

    public void setContent(byte[] content)
    {
        this.content = content;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * br.cin.stadium.middleware.media.ObjectSerializable#marshal(br.cin.stadium
     * .middleware.marshall.MarshallOutputStream)
     */
    public void marshal(MarshallOutputStream mos) throws FormatException
    {
        mos.writeUTF(this.getName());
        mos.writeUTF(this.getType());
        mos.writeInt(this.getStadiumSector());
        mos.write(this.getContent());
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * br.cin.stadium.middleware.media.ObjectSerializable#unmarshal(br.cin.stadium
     * .middleware.marshall.MarshallInputStream)
     */
    public void unmarshal(MarshallInputStream mis) throws FormatException
    {
        this.setName(mis.readUTF());
        this.setType(mis.readUTF());
        this.setStadiumSector(mis.readInt());

        int available = mis.available();
        this.setContent(mis.readBytes(available));

    }
    
    public String toString() {
		String retorno = "NAME = " + this.getName() + "\n" + 
            "TYPE = " + this.getType() + "\n" + 
            "SECTOR = " + this.getStadiumSector() + "\n" + 
            "Sector size = " + ((content==null)?"null":content.length);

		return retorno;
    	
    }

}